<template>
  <div style="width: 100%;">
    <el-form
      :model="createInfo"
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-form-item label="服务组名称" prop="serviceGroupName">
        <div style="width: 95%;">
          <el-input
            v-model="createInfo.serviceGroupName"
            placeholder="请填写服务组名称，系统中保持唯一"
            clearable
            style="width: 100%"
            @blur="checkGroupName"
          ></el-input>
        </div>
      </el-form-item>
      <!--      <el-form-item label="服务组标识" prop="serviceGroupIndicate">-->
      <!--        <el-input-->
      <!--          v-model="createInfo.serviceGroupIndicate"-->
      <!--          placeholder="请填写服务组标识，系统中保持唯一"-->
      <!--          clearable-->
      <!--          style="width: 300px"-->
      <!--        ></el-input>-->
      <!--      </el-form-item>-->
      <el-form-item label="服务组描述" prop="serviceGroupDesc">
        <div style="width: 95%;">
          <el-input
            type="textarea"
            v-model="createInfo.serviceGroupDesc"
            style="width: 100%"
            clearable
            placeholder="请填写服务组描述信息，用于描述服务组功能"
          ></el-input>
        </div>
      </el-form-item>
      <el-form-item>
        <div style="display: flex; justify-content: right; width: 95%;">
          <el-button
            type="primary"
            size="small"
            @click="createServiceGroup('ruleForm')"
            class="el-button-primary-style"
          >立即创建
          </el-button>
          <el-button
            type="primary"
            size="small"
            @click="resetForm()"
            class="el-button-primary-style"
            style="margin-right: 10px;"
          >关闭
          </el-button>
        </div>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
  export default {
    name: "CreateServiceGroupDialog",
    data() {
      return {
        createInfo: {
          serviceGroupName: "",
          // serviceGroupIndicate: "",
          serviceGroupDesc: "",
        },
        rules: {
          serviceGroupName: [
            {required: true, message: "请填写服务组名称", trigger: "blur"},
            {
              min: 3,
              max: 20,
              message: "长度在 3 到 20 个字符",
              trigger: "blur",
            },
            {
              pattern: /^[\u4e00-\u9fa5a-zA-Z0-9-_.]+$/,
              message: "服务组名称包含中文、字母、数字，特殊字符_、-、.",
              trigger: ["blur", "change"],
            },
          ],
          // serviceGroupIndicate: [
          //   {
          //     required: true,
          //     message: "请填写服务组标识，系统中保持唯一",
          //     trigger: "blur",
          //   },
          //   {
          //     min: 3,
          //     max: 20,
          //     message: "长度在 3 到 20 个字符",
          //     trigger: "blur",
          //   },
          // ],
        },
      };
    },
    methods: {
      createServiceGroup(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.$appStoreApi.checkGroupName(this.createInfo.serviceGroupName).then((res) => {
              if (res && res.data === false) {
                this.$router.push({
                  name: "CreateServiceGroup",
                  params: {
                    openType: "create",
                    serviceGroupName: this.createInfo.serviceGroupName,
                    // serviceGroupIndicate: this.createInfo.serviceGroupIndicate,
                    serviceGroupDesc: this.createInfo.serviceGroupDesc,
                  },
                });
              } else {
                this.$message.error("服务组名称重复，请检查");
              }
            })
          } else {
            this.$message.warning("有数据未满足要求，请更改");
            return false;
          }
        });
      },
      checkGroupName() {
        this.$appStoreApi.checkGroupName(this.createInfo.serviceGroupName).then((res) => {
          if (res && res.data === true) {
            this.$message.error("服务组名称重复，请检查");
          }
        })
      },
      resetForm() {
        this.$emit('closeCreateServiceGroupDialog');
        this.$refs.ruleForm.resetFields();
      },
    },
  };
</script>

<style lang="css" scoped>
</style>
